﻿using Gym.Application.Common.Interfaces;
using Microsoft.EntityFrameworkCore;

namespace Gym.Infrastructure.Persistence.Repositories
{
    public class GymsRepository : IGymsRepository
    {
        private readonly GymDbContext _dbContext;

        public GymsRepository(GymDbContext dbContext)
        {
            _dbContext = dbContext;
        }

        public async Task AddGymAsync(Domain.GymAggregate.Gym gym)
        {
            await _dbContext.Gyms.AddAsync(gym);
            await _dbContext.SaveChangesAsync();
        }

        public async Task<bool> ExistsAsync(Guid id)
        {
            return await _dbContext.Gyms.AsNoTracking().AnyAsync(g => g.Id == id);
        }

        public async Task<Domain.GymAggregate.Gym?> GetByIdAsync(Guid id)
        {
            return await _dbContext.Gyms.AsNoTracking().FirstOrDefaultAsync(g => g.Id == id);
        }

        public async Task<List<Domain.GymAggregate.Gym>> ListSubscriptionGymsAsync(Guid subscriptionId)
        {
            return await _dbContext.Gyms
                .AsNoTracking()
                .Where(g => g.SubscriptionId == subscriptionId)
                .ToListAsync();
        }

        public async Task UpdateAsync(Domain.GymAggregate.Gym gym)
        {
            _dbContext.Update(gym);
            await _dbContext.SaveChangesAsync();
        }
    }
}
